package com.shell.guard.exception;

import java.io.PrintWriter;
import java.io.StringWriter;
import org.springframework.http.HttpStatus;

public abstract class AbstractException extends RuntimeException {
	private static final long serialVersionUID = 1L;
	private final HttpStatus status;
	private final int code;
	private final String message;
	private final String detailMessage;

	public AbstractException(HttpStatus status, ErrorCode errorCode) {
		this.status = status;
		this.code = errorCode.getCode();
		this.message = errorCode.getMessage();
		this.detailMessage = errorCode.getDetailMessage();
	}

	public AbstractException(HttpStatus status) {
		this.status = status;
		this.code = 0;
		this.message = null;
		this.detailMessage = null;
	}

	public AbstractException(HttpStatus status, ErrorCode errorCode, Throwable cause) {
		super(cause);
		this.status = status;
		this.code = errorCode.getCode();
		this.message = errorCode.getMessage();
		this.detailMessage = ((cause.getMessage() != null ? cause.getMessage() : "") + toStackTrace(cause));
	}

	private String toStackTrace(Throwable e) {
		StringWriter errorStackTrace = new StringWriter();
		e.printStackTrace(new PrintWriter(errorStackTrace));
		return errorStackTrace.toString();
	}

	public HttpStatus getStatus() {
		return this.status;
	}

	public ErrorCode getCode() {
		return new ErrorCode(this.code, this.message, this.detailMessage);
	}

	public String getMessage() {
		return this.message;
	}

	public String getDetailMessage() {
		return this.detailMessage;
	}
}